<?php
namespace app\common\model;

use think\Model;

class TmpMobile extends Model
{
    protected $table = 'tmp_mobile';


    /*
	 * 开启微信注册模式
     */
    function openWeixinReigster($openid, $scene='wx_register') {
    	return $this->save(['openid'=>$openid]);
    }

    /*
	 * 判断是否注册模式
     */
    function isOpenWeixinRegister($openid, $scene='wx_register') {
    	return $this->where('scene', $scene)
    			->where('is_del', '0')
    			->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours')))
    			->where('openid', $openid)
    			->count();
    }

    /*
	 * 微信注册绑定手机号
     */
    function tmpBindMobile($openid, $mobile, $scene='wx_register') {

    	$lastest = $this->where('openid', $openid)
    	->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours')))
    	->where('scene', $scene)
    	->order('create_time DESC')
    	->find();

    	$lastest->mobile = $mobile;
    	return $lastest->save();
    }

    /*
	 * 获取微信注册，绑定的手机
     */
    function getBindMobile($openid, $scene='wx_register') {
    	$res = $this->where('openid', $openid)
    	->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours')))
    	->where('scene', $scene)
    	->where('is_del', '0')
    	->field('mobile')
    	->order('create_time DESC')
    	->find();

    	return is_null($res) ? false : $res['mobile'];
    }

    /*
	 * 关闭微信注册通道
     */
    function finishRegister($openid) {
    	return $this->where('openid', $openid)
    	->where('create_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours')))
    	->update(['is_del'=>1]);
    }
}